package com.ysx.attendance.controller;


import com.alibaba.fastjson.JSON;
import com.ysx.attendance.bean.ClassBean;
import com.ysx.attendance.entity.ClassEntity;
import com.ysx.attendance.service.ClassService;
import com.ysx.attendance.service.impl.ClassServiceImpl;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name="ClassController",urlPatterns="/class/find")
public class ClassController extends GenericServlet {

    private ClassService classService= new ClassServiceImpl();

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        List<ClassBean> classList =new ArrayList<ClassBean>();
        if("all".equals(method)){
            findAllClass(response);
        }else if("one".equals(method)){
            findOneClass(request, response);
        }else if("insert".equals(method)){

            // 做的任何事情=业务逻辑  把这些学生保存到数据库中  所有的数据都封装在request中  容器=管家

            // 1 把所有学生的id数据拿到和哪天迟到的数据拿到 [10010 10020]


            String[] ids = request.getParameterValues("ids");

            //迟到日期
            String delayDate = request.getParameter("delayDate");



            // 2 调用Service把id保存到数据库中

            int rs = classService.saveData(ids,delayDate);
            // 3 拿到Service的结果 然后返回JSON数据给前端?
            Map<String,Object> map = new HashMap<>();
            if(rs ==1){
                map.put("code",200);
                map.put("message","success");
            }else{
                map.put("code",500);
                map.put("message","failure");
            }
            String jsonString =  JSON.toJSONString(map);
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter writer = response.getWriter();
            writer.print(jsonString);
            writer.flush();
        }
    }

    private void findAllClass(ServletResponse response) throws IOException {
        List<ClassBean> classList;

        //现在先写死 后面写或 后面这个值从session中取
        Integer userId=10010;

        classList = classService.findClassList(userId);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","success");
        map.put("data",classList);

        String jsonString =  JSON.toJSONString(map);
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.print(jsonString);
        writer.flush();
    }

    private void findOneClass(ServletRequest request, ServletResponse response) throws IOException {
        // 重点: 在控制层中只做2件事  分层=分工
        //1 接受参数
        String ps = request.getParameter("classId");
        if(null != ps && "".equals(ps) && "null".equals(ps) && "".equals(ps.trim())){
            return;
        }
        Integer classId= Integer.parseInt(ps);
        String studentName= request.getParameter("studentName");

        ClassBean classBean=null;
        if("".equals(studentName)){
            classBean =classService.findTeacherAndStudentListByClassId(classId);
        }else{
            classBean =classService.findTeacherAndStudentListByClassIdAndStudentName(classId,studentName);
        }

        //2 调用Service拿到结果并返回给前端  注意:不去调用service 然后在Controller把代码写完

        List<ClassBean> classList = new ArrayList<>();
        classList.add(classBean);

        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("message","success");
        map.put("data",classList);

        String jsonString =  JSON.toJSONString(map);
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.print(jsonString);
        writer.flush();
    }
}
